﻿using Newtonsoft.Json;

namespace RAP.Web.Libary.Common.Model
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ServiceResult<T>
    {
        /// <summary>
        /// 空数据对象
        /// </summary>
        public static ServiceResult<T> Empty { get; } = new ServiceResult<T> { errorCode = -1, errorMessage = "服务器内部错误！" };

        /// <summary>
        /// 数据对象
        /// </summary>
        public T data { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public int errorCode { get; set; } = 0;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string errorMessage { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ServiceResult<T> CreateSuccessResult ( T data )
        {
            return new ServiceResult<T>() { data = data };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString ()
        {
            return JsonConvert.SerializeObject( this );
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ServiceResult : ServiceResult<object>
    {
        /// <summary>
        /// 
        /// </summary>
        public static new ServiceResult Empty { get; } = new ServiceResult { errorCode = -1, errorMessage = "服务器内部错误！" };

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public static ServiceResult CreateErrorResult ( string errorMsg, int errorCode )
        {
            return new ServiceResult { errorCode = errorCode, errorMessage = errorMsg };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public new static ServiceResult CreateSuccessResult ( object data )
        {
            return new ServiceResult() { data = data };
        }
    }
}
